<?php

class Products_m extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    //PRODUCTS
    public function get_products($cat=null,$page=0){
        $offset = 20;
        $start = $page*$offset;
        $this->db->limit($offset,$start);
        $this->db->select('products.*');
        $this->db->select('products_cat.products_cat_title');
        $this->db->select('designers.nick_name');
        $this->db->select('designers.full_name');
        $this->db->order_by('modified_date','desc');
        $this->db->join('products_cat', 'category = products_cat_id','left outer');
        $this->db->join('designers', 'designer = designers.id','left outer');
        if(!empty($cat)){
            $this->db->where('category',$cat);
            $this->db->or_where('products_cat_parent',$cat);
        }
        return $this->db->get('products');
    }
    
    public function get_products_front($cat=null,$page=0){
        $child = $this->get_child_cat($cat);
        $offset = 21;
        $start = $page*$offset;
        $this->db->limit($offset,$start);
        $this->db->select('products.*');
        $this->db->select('products_cat.products_cat_title');
        $this->db->join('products_cat', 'category = products_cat_id','left outer');
        if(!empty($cat)){
            $this->db->where_in('category',$child);
        }
        $this->db->order_by('id','desc');
        return $this->db->get('products');
    }
    
    public function get_products_count(){
        return $this->db->count_all_results('products');
    }
    
    public function get_product($id){
        $this->db->join('products_cat', 'category = products_cat_id','left outer');
        $this->db->where('id',$id,false);
        $result = $this->db->get('products');
        if($result->num_rows() > 0){
            return $result->first_row();
        }else{
            return null;
        }
    }
    
    public function get_product_front($id){
        $this->db->select('products.*');
        $this->db->select('products_cat.products_cat_title');
        $this->db->select('designers.nick_name');
        $this->db->select('designers.full_name');
        $this->db->select('designers.id as designer_id');
        $this->db->where('products.id',$id,false);
        $this->db->join('products_cat', 'category = products_cat_id','left outer');
        $this->db->join('designers', 'designer = designers.id','left outer');
        $result = $this->db->get('products');
        if($result->num_rows() > 0){
            $product = $result->first_row();
            $product->photo = $this->get_product_photo($id);
            return $product;
        }else{
            return null;
        }
    }
    
    public function get_product_photo($id){
        $this->db->where('products_id',$id,false);
        $this->db->order_by('products_id');
        return $this->db->get('products_photo');
    }
    
    public function search_product($name){
        
    }
    
    public function add($input){
        //another field
        $this->db->set('name',$input['name']);
        $this->db->set('category',$input['category'],FALSE);
        $this->db->set('main_photo',$input['main_photo']);
        $this->db->set('description',$input['description']);
        $this->db->set('price',$input['price'],FALSE);
        $this->db->set('quantity',$input['quantity'],FALSE);
        $this->db->set('designer',$input['designer']);
        $this->db->set('dimention',$input['dimention']);
        $this->db->set('materials',$input['materials']);
        $this->db->set('year',$input['year'],FALSE);
        $this->db->set('qty_status',$input['qty_status'],FALSE);
        $this->db->set('modified_date','NOW()',FALSE);
        $this->db->insert('products');
    }
    public function edit($input){
        //main image
        if(!empty($input['main_photo'])){
            $this->delete_main_photo($input['id']);
            $this->db->set('main_photo',$input['main_photo']);
        }
        //another field
        $this->db->set('name',$input['name']);
        $this->db->set('category',$input['category'],FALSE);
        $this->db->set('description',$input['description']);
        $this->db->set('price',$input['price'],FALSE);
        $this->db->set('quantity',$input['quantity'],FALSE);
        $this->db->set('designer',$input['designer']);
        $this->db->set('dimention',$input['dimention']);
        $this->db->set('materials',$input['materials']);
        $this->db->set('year',$input['year'],FALSE);
        $this->db->set('qty_status',$input['qty_status'],FALSE);
        $this->db->set('modified_date','NOW()',FALSE);
        $this->db->where('id',$input['id']);
        $this->db->update('products');
    }
    
    public function delete($id){
        $this->photo_delete_by_products($id);
        $this->delete_main_photo($id);
        $this->db->where('id',$id,FALSE);
        $this->db->delete('products');
    }
    
    public function delete_main_photo($products_id){
        $this->db->where('id',$products_id,FALSE);
        $product = $this->db->get('products')->first_row();
        $path ='./photo/products/main/'.$product->main_photo;
        unlink($path);
    }
    
    //CATEGORIES
    
    function get_parent_cat($child_id){
        $this->db->where('products_cat_id',$child_id);
        return $this->db->get('products_cat')->first_row()->products_cat_parent;
    }
    
    function get_child_cat($parent_id){
        if(empty($parent_id)){
            return null;
        }
        $result = array();
        $result[$parent_id] = $parent_id;
        $child = $this->get_categories($parent_id);
        foreach($child->result() as $c){
            $result[$c->products_cat_id] = $c->products_cat_id;
            $cc = $this->get_categories($c->products_cat_id);
            foreach($cc->result() as $item){
                $result[$item->products_cat_id] = $item->products_cat_id;
            }
        }
        return $result;
    }
    
    function get_categories($parent_id=0){
        $this->db->where('products_cat_parent',$parent_id);
        return $this->db->get('products_cat');
    }
    
    function get_products_cat(){
        $this->db->order_by('products_cat_title','ASC');
        return $this->db->get('products_cat');
    }
    
    function validate_cat_id($cat_id){
        $this->db->where('products_cat_id',$cat_id);
        $result = $this->db->get('products_cat');
        return ($result->num_rows()>0)?true:false;
    }
    
    function get_designers_list(){
        $this->db->select('id');
        $this->db->select('nick_name');
        $this->db->select('full_name');
        $this->db->order_by('nick_name','asc');
        return $this->db->get('designers');
    }
    
    //PHOTO
    public function photo_add($input){
        //set
        $this->db->set('products_id',$input['products_id'],FALSE);
        $this->db->set('link',$input['photo']);
        $this->db->insert('products_photo');
    }
    
    public function photo_delete($id){
        $this->db->where('id',$id);
        $result = $this->db->get('products_photo');
        $path ='./photo/products/'.$result->first_row()->link;
        $this->db->where('id',$id);
        $this->db->delete('products_photo');
        unlink($path);
    }
    
    public function photo_delete_by_products($products_id){
        $this->db->where('products_id',$products_id);
        $result = $this->db->get('products_photo');
        foreach($result->result() as $photo){
            $this->photo_delete($photo->id);
        }
    }
    
    //BREADCUMBS
    function get_breadcumbs($cat_id){
        $cumb = '';
        $curr = true;
        while($cat_id != 0){
            $this->db->where('products_cat_id',$cat_id);
            $result = $this->db->get('products_cat')->first_row();
            if($curr){
                $cumb = ' - '.$result->products_cat_title.$cumb;
                $curr = false;
            }
            else{
                $cumb = ' - <a href="'.base_url().'products/cat/'.$result->products_cat_id.'">'.$result->products_cat_title.'</a>'.$cumb;
            }
            $cat_id = $result->products_cat_parent;
        }
        $cumb = '<a href="'.base_url().'products">PRODUCTS</a>'.$cumb;
        return $cumb;
    }
    
}